package org.gexperiments;

import java.awt.BasicStroke;
import java.awt.Dimension;
import java.awt.Stroke;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;

import org.apache.commons.collections15.Transformer;
import org.gexperiments.graph.ImageRenderer;
import org.gexperiments.graph.Link;
import org.gexperiments.graph.Node;

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.shortestpath.DijkstraShortestPath;
import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.control.ModalGraphMouse;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
import edu.uci.ics.jung.visualization.picking.ShapePickSupport;
import edu.uci.ics.jung.visualization.renderers.Renderer.VertexLabel.Position;

/**
 * Hello world!
 *
 */
public class App {
	public static void main(String[] args) {

		Graph<Node, Link> g = new DirectedSparseMultigraph<Node, Link>();

		Node n1 = new Node("10.10.67.1", "Ip10-67.1");
		Node n2 = new Node("10.10.67.2", "Ip10-67.2");
		Node n3 = new Node("10.10.67.3", "Ip10-67.3");
		Node n4 = new Node("10.10.67.4", "Ip10-67.4");
		Node n5 = new Node("10.10.67.5", "Ip10-67.5");
		Node n6 = new Node("10.10.67.6", "Ip10-67.6");
		Node n7 = new Node("10.10.67.7", "Ip10-67.7");
		Node n8 = new Node("10.10.67.8", "Ip10-67.8");
		Node n9 = new Node("10.10.67.9", "Ip10-67.9");
		Node n10 = new Node("10.10.67.10", "Ip10-67.10");
		Node n11 = new Node("10.10.67.11", "Ip10-67.11");

		ArrayList<Node> vertices = new ArrayList<Node>();
		vertices.add(n1);
		vertices.add(n2);
		vertices.add(n3);
		vertices.add(n4);
		vertices.add(n5);
		vertices.add(n6);
		vertices.add(n7);
		vertices.add(n8);
		vertices.add(n9);
		vertices.add(n10);
		vertices.add(n11);

		ArrayList<Link> links = new ArrayList<Link>();
		Link l1 = new Link("Link.1-2", n1, n2, 48);
		links.add(l1);

		Link l2 = new Link("Link.2-3", n2, n3, 48);
		links.add(l2);

		Link l3 = new Link("Link.3-4", n3, n4, 48);
		links.add(l3);

		Link l4 = new Link("Link.4-5", n4, n5, 48);
		links.add(l4);

		Link l5 = new Link("Link.5-6", n5, n6, 48);
		links.add(l5);

		Link l6 = new Link("Link.6-7", n6, n7, 48);
		links.add(l6);

		Link l7 = new Link("Link.7-8", n7, n8, 48);
		links.add(l7);

		Link l8 = new Link("Link.8-9", n8, n9, 48);
		links.add(l8);

		Link l9 = new Link("Link.9-10", n9, n10, 48);
		links.add(l9);

		Link l10 = new Link("Link.10-11", n10, n11, 48);
		links.add(l10);

		Link l11 = new Link("Link.1-11", n1, n11, 48);
		links.add(l11);

		for (Node n : vertices) {
			g.addVertex(n);
		}

		for (Link l : links) {
			g.addEdge(l, l.getaEnd(), l.getzEnd(), EdgeType.DIRECTED);
		}

		// g.addEdge(new Link("RandomLink", links.get(3).getaEnd(),
		// links.get(5).getaEnd(), 56), links.get(3).getaEnd(),
		// links.get(5).getaEnd(), EdgeType.DIRECTED);

		DijkstraShortestPath<Node, Link> alg = new DijkstraShortestPath<Node, Link>(g);
		List<Link> path = new ArrayList<Link>(alg.getPath(n2, n6));
		System.out.println(path);

		// SimpleGraphView sgv = new SimpleGraphView(); //We create our graph in
		// here
		// The Layout<V, E> is parameterized by the vertex and edge types
		Layout<Node, Link> layout = new FRLayout<Node, Link>(g);
		layout.setSize(new Dimension(600, 600)); // sets the initial size of the
													// space
		// The BasicVisualizationServer<V,E> is parameterized by the edge types
		VisualizationViewer<Node, Link> vv = new VisualizationViewer<Node, Link>(layout);
		vv.setPreferredSize(new Dimension(550, 550));
		// vv.getRenderContext().setVertexLabelTransformer(new
		// ToStringLabeller<Node>());
		vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller<Link>());
		vv.getRenderer().getVertexLabelRenderer().setPosition(Position.S);
		vv.getRenderer().setVertexRenderer(new ImageRenderer());
		// vv.getRenderer().setEdgeRenderer(new ReshapingEdgeRenderer<Node,
		// Link>());

		float dash[] = { 10.0f };
		final Stroke edgeStroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 10.0f, dash,
				0.0f);
		Transformer<Link, Stroke> edgeStrokeTransformer = new Transformer<Link, Stroke>() {
			@Override
			public Stroke transform(Link s) {
				return edgeStroke;
			}
		};
		vv.getRenderContext().setEdgeStrokeTransformer(edgeStrokeTransformer);

		DefaultModalGraphMouse<Node, Link> gm = new DefaultModalGraphMouse<Node, Link>();
		gm.setMode(ModalGraphMouse.Mode.TRANSFORMING);
		vv.setGraphMouse(gm);

		ShapePickSupport<Node, Link> pickSupport = new ShapePickSupport<Node, Link>(vv);
		vv.setPickSupport(pickSupport);

		JFrame frame = new JFrame("Simple Graph View");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().add(vv);
		frame.pack();
		frame.setVisible(true);
	}
}
